﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Mobile.Mvc;
using MultiPanelTest.Data;

namespace MultiPanelTest.Search
{
    public partial class SearchView : ViewControl, IView<Products>
    {
        [PublishEvent("OnSearch")]
        public event EventHandler<DataEventArgs<string>> OnSearchEvent;

        [PublishEvent("OnSelected")]
        public event EventHandler<DataEventArgs<Product>> OnSelectedEvent;

        [PublishEvent("OnBack")]
        public event EventHandler OnBackEvent;

        [PublishEvent("OnViewLoaded")]
        public event EventHandler ViewLoadedEvent;    

        public SearchView()
        {
            InitializeComponent();
        }

        private void OnProductsUpdated(object sender, EventArgs e)
        {
            this.lstProducts.DataSource = this.ViewData.Model.ProductList.ToList();
            this.lstProducts.DisplayMember = "ProductName";
        }


        #region IView<Products> Members

        public new ViewDataDictionary<Products> ViewData
        {
            get;
            set;
        }

        public new Products Model
        {
            get;
            set;
        }

        #endregion

        public void Show()
        {
            this.Visible = true;
            ((Form)this.Parent).Menu.MenuItems[1].Text = "";
            ((Form)this.Parent).Menu.MenuItems[0].Text = "Back";
            ((Form)this.Parent).Menu.MenuItems[0].Click += new EventHandler(Back_Click);
        }

        public void Hide()
        {
            // Remove event handler
            ((Form)this.Parent).Menu.MenuItems[0].Click -= new EventHandler(Back_Click);
            this.Visible = false;
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            if (OnSearchEvent != null)
            {
                // Raise the event and notify the controller
                OnSearchEvent(this, new DataEventArgs<string>(txtSearch.Text));
            }
        }

        private void Back_Click(object sender, EventArgs e)
        {
            if (this.OnBackEvent != null)
            {
                this.OnBackEvent(this, EventArgs.Empty);
            }
        }       

        private void txtSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (OnSearchEvent != null)
            {
                // Raise the event and notify the controller
                OnSearchEvent(this, new DataEventArgs<string>(txtSearch.Text));
            }
        }

        private void menuItemSelect_Click(object sender, EventArgs e)
        {
            if (this.lstProducts.SelectedItem != null)
            {
                Product product = this.lstProducts.SelectedItem as Product;
                if (this.OnSelectedEvent != null)
                {
                    this.OnSelectedEvent(this, new DataEventArgs<Product>(product));
                }
            }


        }

        private void SearchForm_Load(object sender, EventArgs e)
        {
            if (this.ViewLoadedEvent != null)
            {
                this.ViewLoadedEvent(this, EventArgs.Empty);
            }
        }

   
    }
}
